<template>
  <div>
    <a-card style="margin-bottom: 10px">
      <a-row>
        <a-col :span="24">
          <a-form
            layout="inline"
            :form="form"
            @submit="handleSubmit"
          >
            <a-form-item label="姓名">
              <a-input
                allow-clear
                placeholder="请输入姓名"
                v-model="name"
              ></a-input>
            </a-form-item>
            <a-form-item label="工号">
              <a-input
                allow-clear
                placeholder="请输入工号"
                v-model="jobNumberInput"
              ></a-input>
            </a-form-item>
            <a-form-item label="手机号">
              <a-input
                allow-clear
                placeholder="请输入手机号"
                v-model="mobile"
              ></a-input>
            </a-form-item>
            <a-form-item>
              <a-button
                type="primary"
                html-type="submit"
              >搜索</a-button>
              <a-button
                type="primary"
                style="margin-left: 10px"
                @click="add('')"
              >新建</a-button>
            </a-form-item>
          </a-form>
        </a-col>
      </a-row>
    </a-card>
    <a-card>
      <a-table
        bordered
        :loading="loading"
        :pagination="pagination"
        :columns="columns"
        :data-source="dataSource"
        @change="handleTableChange"
      >
        <template
          slot="action"
          slot-scope="txt, record"
        >
          <a
            class="mr-10"
            @click="add(record)"
          >
            编辑
          </a>
          <a-popconfirm
            :title="`确定要删除该项吗?`"
            @confirm="deleteRecord(record)"
          >
            <a class="mr-10">
              删除
            </a>
          </a-popconfirm>
        </template>
      </a-table>
      <create
        v-if="showAdd"
        :accountId="accountId"
        @close="close"
      ></create>
    </a-card>
  </div>
</template>

<script>
import create from './create.vue'
import { postAction } from '@/api/manage'

const columns = [
  {
    title: '姓名',
    dataIndex: 'name'
  },
  {
    title: '工号',
    dataIndex: 'jobNumber'
  },
  {
    title: '手机号',
    dataIndex: 'mobile'
  },
  
  {
    title: '操作',
    width: 150,
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  components: {
    create
  },
  data() {
    return {
      columns,
      urlKey: 'basemgt-material/',
      form: this.$form.createForm(this),
      showAdd: false,
      name: '',
      accountId: '',
      mobile:'',
      jobNumberInput:'',
      loading: false,
      dataSource: [],
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: total => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        }
      }
    }
  },
  mounted() {
    this.loadData()
  },
  computed: {
    jobNumber() {
      return this.$store.getters.userInfo.workNo || ''
    }
  },
  methods: {
    handleTableChange(pagination) {
      this.pagination = pagination
      this.loadData()
    },
    close() {
      this.showAdd = false
      this.loadData()
    },
    add(record) {
      this.showAdd = true
      this.accountId = record.id
    },
    reset() {
      this.groupName = ''
      this.distributeChannel = ''
      this.retailDistribution = ''
      this.loadData()
    },
    handleSubmit(e) {
      e.preventDefault()
      this.loadData(1)
    },
    deleteRecord(record) {
      const params = {
        accountId: record.id,
        jobNumber:this.jobNumber
      }
      try {
        postAction(`${this.urlKey}account/delete`, params).then(res => {
          if (res.code == 200) {
            this.$message.success('删除成功')
            this.loadData()
          } else {
            this.$message.warning(res.msg)
          }
        })
      } catch (error) {
        this.$message.warning('删除失败！')
      } 
    },
    loadData(key) {
      this.loading = true
      const params = {
        current: this.pagination.current,
        size: this.pagination.pageSize,
        name: this.name,
        jobNumber:this.jobNumberInput,
        mobile:this.mobile
      }
      if (key) {
        params.current = 1
      }
      try {
        postAction(`${this.urlKey}account/page`, params).then(res => {
          if (res.code == 200) {
            this.dataSource = res.data.records
            this.pagination.total = res.data.total
          } else {
            this.$message.warning(res.msg || res.message)
          }
        })
      } catch (error) {
        this.$message.warning('获取列表失败！')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>
